// pages/my-info/my-info.js
import request from '../../request/index'
//使用 async await 语法必须引入，固定写法。
import regeneratorRuntime from '../../utils/runtime'
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    isShowUpdatearea:false,
    isShowupdateGender:false,
    isShowupdateNickname:false,
    isShowupdateSignature:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取用户信息
    const userInfo = App.globalData.userInfo
    this.setData({userInfo})
  },
  //更新头像
  async updateAvatar() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original','compressed'],
      sourceType: ['album','camera'],
      success: result => {
        this.data.userInfo.avatarUrl = result.tempFilePaths[0]
        //标识用户信息是否发生变化
        App.globalData.userInfo.isUpdate = true
        this.setData({userInfo:this.data.userInfo})
        console.log("暂无接口，只能临时更换一下")
      }
    })
  },
  //打开更新信息区域
  openUpdateArea(event) {
    //使用事件冒泡机制
    const {id} = event.target
    switch (id) {
      case "gender":
        this.setData({
          isShowupdateGender:true,
          isShowUpdatearea:true
        })
        break;
      case "nickname":
        this.setData({
          isShowupdateNickname:true,
          isShowUpdatearea:true
        })
      break;
      case "signature":
        this.setData({
          isShowupdateSignature:true,
          isShowUpdatearea:true
        })
      break;
      case "birthday":
        wx.showToast({
          title: '暂未实现~',
          icon: 'none',
          duration: 1500,
          mask: false,
        })
      break; 
      default:
        break;
    }
  },

  //更新性别
  async updateGender(event){
    const {id} = event.currentTarget
    const newgerder = id === "male" ? 1 : 2
    const userInfo = this.data.userInfo
    if(userInfo.gender === newgerder){
      this.setData({
        isShowUpdatearea:false,
        isShowupdateGender:false
      })
      return
    }
    let {
      gender,birthday,nickname,province,city,signature
    } = this.data.userInfo
    gender = newgerder
    const result = await request({
      url:"user/update",
      data:{gender,birthday,nickname,province,city,signature}
    })
    if(result.code !== 200){
      this.setData({isShowUpdatearea:false})
      wx.showToast({
        title: '更新失败~',
        icon: 'none',
        duration: 1500,
        mask: false,
      })
      return
    }
    this.data.userInfo.gender = newgerder
    App.globalData.userInfo.isUpdate = true
    this.setData({
      userInfo:this.data.userInfo,
      isShowUpdatearea:false,
      isShowupdateGender:false
    })
  },
  //修改昵称
  async nicknameConfirm(event) {
    const {value} = event.detail
    let {
      gender,birthday,nickname,province,city,signature
    } = this.data.userInfo
    nickname = value
    const result = await request({
      url:"user/update",
      data:{gender,birthday,nickname,province,city,signature}
    })
    if(result.code !== 200){
      this.setData({
        isShowUpdatearea:false,
        isShowupdateNickname:false
      })
      wx.showToast({
        title: '更新失败~',
        icon: 'none',
        duration: 1500,
        mask: false,
      })
      return
    }
    this.data.userInfo.nickname = value
    App.globalData.userInfo.isUpdate = true
    this.setData({
      userInfo:this.data.userInfo,
      isShowUpdatearea:false,
      isShowupdateNickname:false
    })
  },
  //修改签名
  async signatureConfirm(event) {
    const {value} = event.detail
    let {gender,birthday,nickname,province,city,signature} = this.data.userInfo
    signature = value
    const result = await request({
      url:"user/update",
      data:{gender,birthday,nickname,province,city,signature}
    })
    if(result.code !== 200){
      this.setData({
        isShowUpdatearea:false,
        isShowupdateSignature:false
      })
      wx.showToast({
        title: '更新失败~',
        icon: 'none',
        duration: 1500,
        mask: false,
      })
      return
    }
    this.data.userInfo.signature = value
    App.globalData.userInfo.isUpdate = true
    this.setData({
      userInfo:this.data.userInfo,
      isShowUpdatearea:false,
      isShowupdateSignature:false
    })
  },
  //阻止事件传递
  stopPass() {

  },
  //关闭更新区域
  closeUpdatearea() {
    this.setData({
      isShowUpdatearea:false,
      isShowupdateGender:false,
      isShowupdateNickname:false,
      isShowupdateSignature:false,
    })
  },


  //退出登录
  layout() {
    wx.showModal({
      title: '确认退出?',
      showCancel: true,
      cancelText: '取消',
      confirmText: '确定',
      success: (result) => {
        if(result.confirm){
          wx.clearStorageSync();
          wx.reLaunch({url: '/pages/login/login'})
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})